<template>
  <div>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix box-title">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- 分类的表格区域 -->
      <el-table :data="list" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="obj">
            <el-button type="primary" size="mini" @click="showEditDialog(obj.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="remove(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 对话框区域 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
      @closed="onaddClosed"
      :close-on-click-modal="false"
    >
      <!-- 表单区域 -->
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addFormRef">
        <el-form-item label="文章名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="文章别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="addList" size="mini">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改对话框 -->
    <!-- 修改的对话框 -->
    <el-dialog title="修改分类" :visible.sync="editVisible" width="35%" @closed="onEditClose">
      <!-- 修改的表单 -->
      <el-form :model="editForm" label-width="80px" :rules="addFormRules" ref="editFormRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="updateList" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI, addListAPI, updateList, removeList } from '@/API/artCate.js'
export default {
  name: 'ArtList',
  data() {
    return {
      list: [],
      // 控制对话框显示隐藏
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 正则规则校验
      addFormRules: {
        cate_name: [
          { required: true, message: '文章名称是必填项', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '文章分类名字必须是1-10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '文章名称是必填项', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '用户名必须是1-10位字母数字', trigger: 'blur' }
        ]
      },
      // 控制修改对话框的显示与隐藏
      editVisible: false,
      // 修改表单的数据对象
      editForm: {
        cate_name: '',
        cate_alias: ''
      }
    }
  },
  created() {
    this.initList()
  },
  methods: {
    async initList() {
      const { data: res } = await getListAPI()
      if (res.code !== 0) return this.$message.error(res.$message)
      this.list = res.data
    },
    // 重置对话框数据
    onaddClosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 添加图书功能
    addList() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 成功以后调用接口
        const { data: res } = await addListAPI(this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 关闭对话框
        this.addVisible = false
        // 重新渲染调用页面表格
        this.initList()
      })
    },
    // 点击修改按钮，展示修改的对话框
    showEditDialog(row) {
      if (row.id === 1 || row.id === 2) {
        this.$message.error('管理员不允许修改此项')
        return
      }
      this.editForm = { ...row }
      this.editVisible = true
    },
    // 重置修改分类的表单
    onEditClose() {
      this.$refs.editFormRef.resetFields()
    },
    // 点击确定修改内容
    updateList() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateList(this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.editVisible = false
        this.initList()
      })
    },
    // 删除数据分类
    async remove(id) {
      if (id === 1 || id === 2) {
        this.$message.error('管理员不允许删除此项')
        return
      }
      const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果result === 'cancel'则代表取消操作
      if (result === 'cancel') return
      console.log(result)
      // 点击确定则result === 'confirm'则进行调用接口删除数据
      const { data: res } = await removeList(id)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.initList()
      this.editVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.box-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
